package chat.dao;

import java.sql.Connection;
import java.sql.ResultSet;

import com.mysql.jdbc.Statement;

import chat.main.ConnectionManager;
import chat.model.User;

public class UserDAO {
	public static User currentUser = null;
	// check user login
	public static User userLogin(String username, String password) {
		ResultSet rs = null;
		java.sql.Statement stm = null;
		Connection currentCon = null;
		String query = "select * from user where user_name='" + username
				+ "' and password='" + password + "'";
		try {
			currentCon = ConnectionManager.getConnection();
			stm = currentCon.createStatement();
			rs = stm.executeQuery(query);
			if (rs.next()) {
				currentUser = new User();
				currentUser.setName(username);
				currentUser.setPassword(password);
			}
		} catch (Exception e) {
			e.getStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception e) {
					rs = null;
				}
			}
			if (stm != null) {
				try {
					stm.close();
				} catch (Exception e) {
					rs = null;
				}
			}
			if (currentCon != null) {
				try {
					currentCon.close();
				} catch (Exception e) {
					currentCon = null;
				}
			}
		}
		return currentUser;
	}
}
